home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2008 September
/
PCWorld_2008-09_cd.bin
/
v cisle
/
sadanastroju
/
bookmark_previews-0.6.5-fx.xpi
/
chrome
/
content
/
previewServiceUtils.js
< prev
next >
Wrap
Text File
|
2008-05-27
|
2KB
|
45 lines
var PreviewServiceUtils = {
hashString : function(str){
var converter =
Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
// we use UTF-8 here, you can choose other encodings.
converter.charset = "UTF-8";
var result = {};
// data is an array of bytes
var data = converter.convertToByteArray(str, result);
var ch = Components.classes["@mozilla.org/security/hash;1"]
.createInstance(Components.interfaces.nsICryptoHash);
ch.init(ch.SHA1);
ch.update(data, data.length);
var hash = ch.finish(false);
// return the two-digit hexadecimal code for a byte
function toHexString(charCode)
{
return ("0" + charCode.toString(16)).slice(-2);
}
// convert the binary hash data to a hex string.
var s = [toHexString(hash.charCodeAt(i)) for (i in hash)].join("")
return s;
},
getPreviewFile : function(aURL,dir){
if (!dir){
dir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
dir.append("bookmarkpreviews");
}
var file = dir.clone();
var filename = this.hashString(aURL.toLowerCase()).substring(0,40);
file.append(filename);
//dump("getpreviewfile: "+aURL+"|"+aURL.length+"\n"+
// "filename: "+filename+"\n"+
// "path: "+file.leafName+" | "+filename.length+"\n"+
// "exists: "+file.exists()+"\n");
return file;
}
}